/**
 * Created by Administrator on 2017/4/6 0006.
 */
var React = require('react');

var OrderGoods = require('../widget/OrderGoods');
var ajax = require('../tools/ajax');
var Icon = require('../components/Icon');

module.exports = React.createClass({
    getInitialState: function () {
        return {
            order:[],
            orderInfo:[],
            country:"",
            orderGoods:"",
            orderList:"",
            currentIndex:1,
            flag:false,//订单为空
            word:""//提示框文字
        }
    },

    componentDidMount: function () {
        this.getOrderByUserId();
    },
    getOrderByUserId(){
        var p =this;
        var userId = localStorage.getItem("userId");
        ajax("POST","getOrderByUserId",{
            userId:userId,
        },function(dt){
            if(dt && dt.success){
                var objs = dt.data;
                p.setState({orderInfo:objs},()=>{
                    p.getOrderInfo(objs);
                });
            }
        })
    },
    /**获取订单信息*/
    getOrderInfo(orderInfo){
        var p = this;
        var order=[];
        if(orderInfo[0]){
            this.setState({flag: false});
            var currentIndex = p.state.currentIndex;
            switch (currentIndex) {
                case "1" :
                    order = orderInfo;
                    break;
                case "2" :
                    for (var i = 0; i < orderInfo.length; i++) {
                        if (orderInfo[i].order_status == 0 && orderInfo[i].pay_status == 0) {
                            order.push(orderInfo[i]);
                        }
                    }
                    break;
                case "3" :
                    for (var i = 0; i < orderInfo.length; i++) {
                        if (orderInfo[i].order_status == 0 && orderInfo[i].shipping_status == 0 && orderInfo[i].pay_status == 2) {
                            order.push(orderInfo[i]);
                        }
                    }
                    break;
                case "4" :
                    for (var i = 0; i < orderInfo.length; i++) {
                        if (orderInfo[i].order_status == 0 && orderInfo[i].shipping_status == 1 && orderInfo[i].pay_status == 2) {
                            order.push(orderInfo[i]);
                        }
                    }
                    break;
                case "5" :
                    for (var i = 0; i < orderInfo.length; i++) {
                        if (orderInfo[i].order_status == 1 && orderInfo[i].shipping_status == 2 && orderInfo[i].pay_status == 2) {
                            order.push(orderInfo[i]);
                        }
                    }
                    break;
            }
            p.setState({order:order},()=>{
                for(var i=0;i<order.length;i++) {
                    p.refs["order"+order[i].id].getOrderGoods();
                }
            });
        }else {
            this.setState({flag:true});
        }
    },
    //btn1功能
    btn1(e){
        var p=this;
        var btn1 = e.currentTarget.getAttribute("data-btn1");
        console.log(btn1);
        var orderId = e.currentTarget.getAttribute("data");
        var user_id = localStorage.getItem("userId");
        if(btn1 == "查看物流") {
            location.href = "#logistics";
            localStorage.setItem("logisticsOrderId",orderId);
        };
        if(btn1 == "取消订单"){
            var orderGoods = p.refs["order"+orderId].state.orderGoods;
            for(var i=0;i<orderGoods.length;i++){
                ajax("POST","updateKucun",{
                    goods_id:orderGoods[i].id,
                    goods_number:-(orderGoods[i].goods_number)
                },function(dt){
                    if(dt&&dt.success){
                        console.log("库存增加");
                    }
                });
            }
            ajax("POST","deleteOrderInfo",{
                id:orderId,
                user_id:user_id,
            },function(dt){
                if(dt&&dt.success){
                    p.setState({word:'订单已删除'},()=>{
                        p.props.MsgBox();
                    });
                    window.location.reload(true);
                }
            })
        };
    },
    //btn2功能
    btn2(e){
        var p=this;
        var btn2 = e.currentTarget.getAttribute("data-btn2");
        console.log(btn2);
        var orderId = e.currentTarget.getAttribute("data");
        var user_id = localStorage.getItem("userId");
        if(btn2 == "去支付") {
            location.href = "#orderInfo";
            localStorage.orderId = orderId;
        };
        if(btn2 == "评论"){
            location.href = "#evaluate";
        }
        if(btn2 == "确认收货"){
            ajax("POST","updateShippingStatus",{
                id:orderId,
                user_id:user_id,
                order_status:1,
                shipping_status:2
            },function(dt){
                console.log(dt);
                if(dt&&dt.success){
                    console.log("确认收货");
                    window.location.reload(true);
                }
            })
        }
    },
    render: function () {
        var p =this;
        var order = p.state.order;
        if(order[0]) {
            var orderList = order.map(function (obj, index) {
                var status = "";
                var btn1 = "";
                var btn2 = "";
                if (obj.order_status === 1 && obj.comment_status === 1) {
                    status = "交易成功";
                    btn1 = "查看物流";
                    btn2 = "评论";
                }
                if (obj.pay_status === 0) {
                    status = "待付款";
                    btn1 = "取消订单";
                    btn2 = "去支付";
                }
                if (obj.shipping_status === 0 && obj.pay_status === 2) {
                    status = "待发货";
                    btn1 = "查看物流";
                    btn2 = "";
                }
                if (obj.shipping_status === 1) {
                    status = "待收货";
                    btn1 = "查看物流";
                    btn2 = "确认收货";
                }
                if (obj.shipping_status === 2) {
                    status = "已收货";
                    btn1 = "订单完成";
                }
                return (
                    <div key={index}>
                        <hr className="orderHr" style={{display:index?"block":"none"}}/>
                        <div className="orderPro">
                            <div className="proTitle">
                                <p className="country">
                                    <span className="orderCode">订单编号:</span>{obj.order_sn}
                                </p>
                                <p className="status">{status}</p>
                            </div>
                            <OrderGoods ref={"order"+obj.id} id={obj.id}/>
                            <div className="proPrice">
                                <p>
                                    实付 <span className="price">¥{obj.order_amount}</span>
                                    <span style={{display:obj.order_point?"inline-block":"none"}}>+{obj.order_point}积分</span>
                                </p>
                            </div>
                            <div className="oderButton">
                                <div className="more btnStyle" onClick={p.btn1} data={obj.id}
                                     data-btn1={btn1}>{btn1}</div>
                                <div className="evaluate btnStyle" style={{display:btn2?"block":"none"}}
                                     onClick={p.btn2} data={obj.id} data-btn2={btn2}>{btn2}</div>
                            </div>
                        </div>
                    </div>
                )
            });
        }
        return (
            <div>
                <div className="empty" style={{display:this.state.flag?"block":"none"}}>
                    <Icon icon="#icon-icon04"></Icon>
                    <p>您还没有订单哦！</p>
                </div>
                {orderList}
            </div>
        )
    }
});

